program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  arr=array of integer;
var
  mass: array of integer;
  ver: array of arr;
  i, j, n, m, x, a, b, c, min, sum, k, l: integer;
  fin, fout: textfile;
begin
  assignfile(fin, 'input.txt');
  reset(fin);
  assignfile(fout, 'output.txt');
  rewrite(fout);
  readln(fin, n, m);
  setlength(mass, n);
  setlength(ver, n);
  for i:=0 to n-1 do
    setlength(ver[i], n);
  for i:=0 to m-1 do
  begin
    readln(fin, a, b, c);
    ver[a-1, b-1]:=c;
    ver[b-1, a-1]:=c;
  end;
  for i:=0 to n-1 do
    mass[i]:=0;
  mass[0]:=1;
  a:=0;
  sum:=0;
  i:=1;
  l:=1;
  while (i<>n) do
  begin
    min:=2000000000;
    for j:=0 to n-1 do
    begin                         
      if (mass[j]=1) then
      begin
        for k:=0 to n-1 do    
        begin
          if (min>=ver[j, k]) and (mass[k]=0) and (ver[j, k]<>0) then
          begin
            min:=ver[j, k];
            b:=k;
          end;
        end;
      end;
    end;
    mass[b]:=1;
    sum:=sum+min;
    a:=b;
    i:=i+1;
    l:=l+1;
  end;
  writeln(fout, sum);
  closefile(fin);
  closefile(fout);
end.

 